﻿using System;
using System.Collections.Generic;
using System.Data;
using DevExpress.XtraEditors;
using UniMana.Utils.Data;
using UniMana.Data;
namespace UniMana.Win.Training
{
    public partial class FrmSchedule_SubOpenSubjectClass : DevExpress.XtraEditors.XtraForm
    {
        #region [Khai Báo Biến]
        private DataTable m_dtaClass;
        private int m_iTraining_SubjectOpenRequestKey;
        private DBMain m_DBMain = new DBMain();
        private List<DataTable> m_lFrmChooseClass;
        #endregion

        public FrmSchedule_SubOpenSubjectClass()
        {
            InitializeComponent();
        }

        public FrmSchedule_SubOpenSubjectClass(
            DataTable _dtaClass,
            List<DataTable> _lFrmChooseClass)
        {
            InitializeComponent();
            this.m_lFrmChooseClass = _lFrmChooseClass;
            this.m_dtaClass = _dtaClass;
            LoadData();
        }

        private void LoadData()
        {
            //lueCatalogue_SchoolYear.GetData(m_DBMain.m_DCReadOnly.Catalogue_SchoolYears);
            //lueCatalogue_Term.GetData(m_DBMain.m_DCReadOnly.Catalogue_Terms);

            this.m_dtaClass.Columns.Add("SubjectClassID",typeof(string));
            this.m_dtaClass.Columns.Add("SubjectClassName", typeof(string));
            this.m_dtaClass.Columns.Add("Training_TrainingProgramKey", typeof(int));
            
            this.m_dtaClass.Columns.Add("Catalogue_SchoolYearKey", typeof(int));
            this.m_dtaClass.Columns.Add("Catalogue_TermKey", typeof(int));  
            this.gctTraining_Subject_Class.DataSource = this.m_dtaClass;
            
            this.rluCatalogue_SchoolYear.GetData(
                this.m_DBMain.m_DCReadOnly.Catalogue_SchoolYears.GetSchoolYearRecentNow());

            this.rluCatalogue_Term.GetData(
                this.m_DBMain.m_DCReadOnly.Catalogue_Terms);

            SetTermAndSchool();
            SetSchool_TermKey();

            this.gctTraining_Subject_Class.DataSource = this.m_dtaClass;
        }

        /// <summary>
        /// Loai danh sách sinh viên
        /// </summary>
        private void GetListStudent()
        {
            //int iTrainingProgramKey = 
                //this.grvTraining_Subject_Class.GetFocusedRowCellValue(
                //this.gclTraining_TrainingProgramKey).ToInt();
            //int iTraining_SubjectOpenRequestKey = this.m_iTraining_SubjectOpenRequestKey;
            int iTrainingClassKey = 
                this.grvTraining_Subject_Class.GetFocusedRowCellValue(
                
                this.gclTraining_ClassKey).ToInt();
            /*
            this.gctTraining_SubjectOpenRequest_Teacher_Student.DataSource = 
             * this.m_DBMain.m_DCReadOnly.Training_SubjectOpenRequest_Teacher_Students.GetListByRef(
             * this.m_DBMain.m_DCReadOnly.Training_Students,
                                                                                                                                                                       this.m_DBMain.m_DCReadOnly.Training_Student_Classes,
                                                                                                                                                                       iTraining_SubjectOpenRequestKey, iTrainingClassKey);
            */
            //for (int i = 0; i < this.m_lFrmChooseClass.Count; i++)
            //{
            //    if (this.m_lFrmChooseClass[i].TrainingClassKey == iTrainingClassKey)
            //    {
            //        this.gctTraining_SubjectOpenRequest_Teacher_Student.DataSource = 
            //            this.m_lFrmChooseClass[i].ListStudent;
            //    }
            //}
        }

        private void btnAgree_Click(object sender, EventArgs e)
        {
            Save();
        }

        public DataTable DataClassInfo
        {
            get { return this.m_dtaClass; }
        }

        private void grvTraining_Subject_Class_FocusedRowChanged(
            object sender, 
            DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            GetListStudent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void SetSchool_TermKey()
        {
            for (int i = 0; i < m_dtaClass.Rows.Count; i++)
            {
                //if (m_dtaClass.Rows[i]["SubjectClassID"].ToString() == "")
                {
                    string id = "";
                    string[] schoolyear =(grvTraining_Subject_Class.GetRowCellDisplayText(
                                    i, gclCatalogue_SchoolYearKey).ToString()).Split('-');
                    id += schoolyear[0];//Năm
                    id += ("_" + grvTraining_Subject_Class.GetRowCellDisplayText(
                                    i, gclCatalogue_TermKey).ToString());//Học Kỳ
                    id += ("_" + grvTraining_Subject_Class.GetRowCellDisplayText(
                                    i, gclClassID).ToString() + "_");//Lớp
                    id += (m_dtaClass.Rows[i]["Training_SubjectID"].ToString());
                    m_dtaClass.Rows[i]["SubjectClassID"] = id;
                }
                //if (m_dtaClass.Rows[i]["SubjectClassName"].ToString() == "")
                {
                    string id = "";
                    id += grvTraining_Subject_Class.GetRowCellDisplayText(
                                    i, gclCatalogue_SchoolYearKey).ToString();//Năm
                    id += ("_" + grvTraining_Subject_Class.GetRowCellDisplayText(
                                    i, gclCatalogue_TermKey).ToString());//Học Kỳ
                    id += ("_" + grvTraining_Subject_Class.GetRowCellDisplayText(
                                    i, gclName).ToString() + "_");//Lớp
                    id += ("Môn " + m_dtaClass.Rows[i]["Training_SubjectName"].ToString());
                    m_dtaClass.Rows[i]["SubjectClassName"] = id;
                }
            }
        }

        private void SetTermAndSchool()
        {
            DataTable dt = this.m_dtaClass;
            for (int i = 0; i < this.m_dtaClass.Rows.Count; i++)
            {
                int iTraining_ClassKey = dt.Rows[i]["Training_ClassKey"].ToInt();
                Training_ClassInfo classInfo = new Training_ClassInfo(iTraining_ClassKey);
                int iTermKey = -1;
                int iSchoolYearKey = -1;
                if (classInfo.Properties.Catalogue_CourseKey != null)
                {
                    int iCouserKey = classInfo.Properties.Catalogue_CourseKey.Value;
                    if (rbt_After.Checked)
                    {
                        this.m_DBMain.m_DCReadOnly.Catalogue_Course_SchoolYear_Terms.
                                GetSchoolYear_TermByCourse_Year_After(
                                    iCouserKey,
                                    ref iSchoolYearKey,
                                    ref iTermKey);
                    }
                    else
                    {
                        this.m_DBMain.m_DCReadOnly.Catalogue_Course_SchoolYear_Terms.
                                GetSchoolYear_TermByCourse_Year(
                                    iCouserKey,
                                    ref iSchoolYearKey,
                                    ref iTermKey);
                    }
                    if (iSchoolYearKey != -1)
                    {
                        Catalogue_SchoolYearInfo schoolYearInfo =
                            new Catalogue_SchoolYearInfo(iSchoolYearKey);
                        //dt.Rows[i]["Catalogue_SchoolYearName"] = schoolYearInfo.Properties.Name;
                        dt.Rows[i]["Catalogue_SchoolYearKey"] = iSchoolYearKey;
                    }
                    if (iTermKey != -1)
                    {
                        dt.Rows[i]["Catalogue_TermKey"] = iTermKey;
                    }
                }
            }
        }

        private void rbt_Now_CheckedChanged(object sender, EventArgs e)
        {
            SetTermAndSchool();
            SetSchool_TermKey();
        }

        private void rbt_After_CheckedChanged(object sender, EventArgs e)
        {
            SetTermAndSchool();
            SetSchool_TermKey();
        }

        private void Save(bool mess = false)
        {
            for (int i = 0; i < this.m_dtaClass.Rows.Count; i++)
            {
                string strSubjectClassID = this.m_dtaClass.Rows[i]["SubjectClassID"].ToString();
                Training_Subject_ClassInfo subjectClassInfo = new Training_Subject_ClassInfo(strSubjectClassID, true);
                
                string strSubjectClassName = this.m_dtaClass.Rows[i]["SubjectClassName"].ToString();
                int iSchoolYearKey = this.m_dtaClass.Rows[i]["Catalogue_SchoolYearKey"].ToInt();
                int iTerm = this.m_dtaClass.Rows[i]["Catalogue_TermKey"].ToInt();
                if (subjectClassInfo.ExistInDatabase)
                {
                    int count = -1;
                    count = subjectClassInfo.NumSjC + 1;
                    strSubjectClassID = this.m_dtaClass.Rows[i]["SubjectClassID"].ToString() + "_" + count;
                    strSubjectClassName = this.m_dtaClass.Rows[i]["SubjectClassName"].ToString() + "_" + count;
                }
                 this.m_dtaClass.Rows[i]["SubjectClassID"] =strSubjectClassID;
                 this.m_dtaClass.Rows[i]["SubjectClassName"] =strSubjectClassName;
                 this.m_dtaClass.Rows[i]["Catalogue_SchoolYearKey"] =iSchoolYearKey;
                 this.m_dtaClass.Rows[i]["Catalogue_TermKey"] = iTerm;
                //if (mess)
                //{
                //    XtraMessageBox.Show("Mã lớp học phần " + strSubjectClassID + " Lưu Thành Công");
                //}
                ///xóa ràng buộc về không đánh mã lớp hp
                /*
                if (strSubjectClassID == "" || strSubjectClassName == "")
                {
                    XtraMessageBox.Show("Mã lớp, tên lớp học phần không được để trống");
                    return;
                }
                 */
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void FrmSchedule_SubOpenSubjectClass_Load(object sender, EventArgs e)
        {

            Save(true);
        }
    }
}